/*
 * @Author: zi.yang
 * @Date: 2022-12-03 01:59:48
 * @LastEditors: zi.yang
 * @LastEditTime: 2022-12-04 21:03:00
 * @Description: throw error
 * @FilePath: /promise/lib/utils/throwError.ts
 */
import { isFunction } from './common';

export function throwTypeError(msg: string): TypeError {
  return new TypeError(`[ToyPromise]: ${msg}`);
}

// ES262-25.4.1.6.1 step 8 invalid 'resolve' throws TypeError
// ES262-25.4.1.6.1 step 9 invalid 'reject throws TypeError
// 判断参数列表是否是都是函数
export function useThrowIsNotFunction(...args: Function[] | unknown[]) {
  args.forEach((arg: Function | unknown) => {
    if (isFunction(arg)) return;
    throw throwTypeError('Promise executor has already been invoked with non-undefined arguments');
  });
}

export function throwIsNotIterable(method: String): TypeError {
  return throwTypeError(`Argument of Promise.${method} is not iterable.`);
}

export function throwAggregateError() {
  const error = new Error('No Promise in Promise.any was resolved');
  error.name = 'AggregateError';
  return error;
}
